package com.mask.im.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mask.im.business.model.entity.UserLabel;
import com.mask.im.business.mapper.UserLabelMapper;
import com.mask.im.business.model.dto.UserLabelDTO;
import com.mask.im.business.common.model.RestResponse;
import com.mask.im.business.service.UserLabelService;
import com.mask.im.business.common.util.MaskIMSecurityUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;


/**
 * @author jian
 */
@Service
public class UserLabelServiceImpl extends ServiceImpl<UserLabelMapper, UserLabel> implements UserLabelService {

	@Override
	public RestResponse<List<UserLabelDTO>> getUserLabel() {
		Integer userId = MaskIMSecurityUtils.getUserId();
		UserLabel userLabel = this.baseMapper.selectOne(new LambdaQueryWrapper<UserLabel>()
				.eq(UserLabel::getUserInfoId, userId));
		if (Objects.isNull(userLabel)){
			return RestResponse.ok(new ArrayList<>());
		}
		String[] split = userLabel.getLabelName().split(",");
		List<String> list = Arrays.asList(split);
		List<UserLabelDTO> arrayList = list.stream().map(item -> {
			UserLabelDTO userLabelDTO = new UserLabelDTO();
			userLabelDTO.setId(userLabel.getId());
			userLabelDTO.setLabel(item);
			return userLabelDTO;
		}).distinct().collect(Collectors.toList());
		return RestResponse.ok(arrayList);
	}
}

